import SwiftUI
import SPGlassKit

public struct GlassChatBubbleExample: GlassExample {
    public static let componentID = "glass-chat-bubble"

    /// Fictitious conversation: no model produced these messages.
    @State private var messages = [
        GlassChatMessage(id: "1", role: .system, text: "Sample data — this conversation is fictitious."),
        GlassChatMessage(id: "2", role: .user, text: "Can you summarise the sample quarterly note in two sentences? 🙂"),
        GlassChatMessage(id: "3", role: .assistant, text: "Certainly. The sample note reports steady demand and a slightly lower margin. It expects the next quarter to look similar, with one new product line."),
        GlassChatMessage(id: "4", role: .tool, text: "{\n  \"tool\": \"sample_lookup\",\n  \"rows\": 3,\n  \"a_very_long_key_without_any_space_to_test_wrapping_inside_the_bubble\": true\n}"),
        GlassChatMessage(id: "5", role: .user, text: "هل يمكنك كتابة الخلاصة بالعربية؟"),
        GlassChatMessage(id: "6", role: .assistant, text: "The sample answer is still being writ", state: .streaming),
        GlassChatMessage(id: "7", role: .assistant, text: "", state: .streaming),
        GlassChatMessage(id: "8", role: .assistant, text: "A partial sample answer that stopped.", state: .failed(reason: "Sample failure: the connection was interrupted.")),
    ]
    @State private var lastCopied = "Nothing copied yet"
    @State private var retryCount = 0

    public init() {}

    public var body: some View {
        ScrollView {
            LazyVStack(spacing: 12) {
                ForEach(messages) { message in
                    GlassChatBubble(
                        message,
                        usesMonospacedFont: message.role == .tool,
                        onCopy: { text in lastCopied = "Copied \(text.count) characters (kept inside the example)" },
                        onRetry: { retryCount += 1 }
                    )
                }
                Text("\(lastCopied) · retried \(retryCount) times")
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }
            .padding()
        }
    }
}

#Preview("GlassChatBubble") { GlassChatBubbleExample().glassTheme(.aurora) }
